package com.davy.gridpoint.utils;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;

import com.davy.gridpoint.DavyApp;

import java.util.List;
import java.util.Locale;

public class GPSToGCJ {

    static final double a = 6378245.0;
    static final double ee = 0.00669342162296594323;
    static final double pi = Math.PI;
    static final double x_pi = Math.PI * 3000.0 / 180.0;

    /**
     * WGS-84坐标转GCJ-02坐标  高德位置
     */
    public static double[] wgs84ToGcj02(double wgLat, double wgLon) {
        if (outOfChina(wgLat, wgLon)) {
            return new double[]{wgLat, wgLon};
        }
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = wgLat + dLat;
        double mgLon = wgLon + dLon;
        return new double[]{mgLat, mgLon};
    }

    /**
     * 将 GCJ-02 坐标转换成 BD-09 坐标  百度位置
     */
    public static double[] gcj02ToBd09(double lat, double lon) {
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double bdLon = z * Math.cos(theta) + 0.0065;
        double bdLat = z * Math.sin(theta) + 0.006;
        return new double[]{bdLat, bdLon};
    }

    // World Geodetic System ==> Mars Geodetic System
    //主要调用函数
    public static double[] transformWGS84ToGCJ(double wgLon, double wgLat) {
        double[] d = new double[2];
        double mgLat;
        double mgLon;
        if (outOfChina(wgLat, wgLon)) {
            mgLat = wgLat;
            mgLon = wgLon;
        }
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        mgLat = wgLat + dLat;
        mgLon = wgLon + dLon;
        d[0] = mgLon;
        d[1] = mgLat;
        return d;
    }

    //中国之外则不需要转换
    static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    //获取地址信息:城市、街道等信息
    public static List<Address> getAddress(Location location) {
        List<Address> result = null;
        try {
            if (location != null) {
                Geocoder gc = new Geocoder(DavyApp.getContext(), Locale.getDefault());
                //35.284864,113.670044
                result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                LogUtils.e("TAG", "获取地址信息：" + result.toString());
                LogUtils.e("TAG", "获取地址信息：" + location.getAltitude() + ",,," + location.toString());
                if (result.size() > 0) {
                    LogUtils.e("TAG", "获取地址信息：" + result.get(0).getAddressLine(0));
                    LogUtils.e("TAG", "获取地址信息：" + result.get(0).getAddressLine(1));
                    String countryName = result.get(0).getCountryName();//中国
                    String admin = result.get(0).getAdminArea();//河南省
                    String locality = result.get(0).getLocality();//郑州市
                    String thoroughfare = result.get(0).getThoroughfare();//东风南路
                    String roadworkAdress = result.get(0).getAddressLine(1);
                    LogUtils.e("tag", "countryName:" + countryName + "admin:" + admin + "locality:" + locality + ",thoroughfare:" + thoroughfare);
                    String LocaAdress = result.get(0).getAddressLine(0) + "" + result.get(0).getAddressLine(1);
                    LogUtils.e(result.get(0).getAddressLine(0) + "" + result.get(0).getAddressLine(1));
                    LogUtils.e(location.getAltitude() + "米");
                    LogUtils.e("LocaAdress:" + LocaAdress);
                    LogUtils.e("roadworkAdress:" + roadworkAdress);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
